package EBM.BLL;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import EBM.DB.BugDAO;
import EBM.DB.ProjectDAO;
import EBM.DB.ProjectModelDAO;
import EBM.DB.ProjectUserDAO;
import EBM.model.Model;
import EBM.model.Project;
import EBM.model.ProjectUser;
import EBM.model.User;

public class ProjectManage {
	private List<Object> tmp;
	private List<Object> response;
	private ProjectDAO projectDAO;
	private ProjectModelDAO modelDAO;
	private ProjectUserDAO projectUserDAO;
	private BugDAO bugDAO;
	private boolean result;
	
	//得到和某个用户相关的项目
	public List<Object> GetProjectByUserid(List<Object> request) 
	throws SQLException, ClassNotFoundException{
		
		response = new ArrayList<Object>();
		
		String userid = request.get(0).toString();
		
		projectDAO = new ProjectDAO();	
		tmp = projectDAO.findByuserid(userid);
		
		Iterator<Object> iter = tmp.iterator();
		while(iter.hasNext()){
			Project p = new Project();
			p = (Project)iter.next();
			response.add(p.getProid());
			response.add(p.getName());			
		}
		//没有相关的项目
		if(tmp.size()<1){
			response.add("-1");
		}
		
		return response;
	}
	
	//得到所有项目
	public List<Object> GetAllProject(List<Object> request) 
	throws SQLException, ClassNotFoundException{
		
		//String userid = request.get(0).toString();
		
		projectDAO = new ProjectDAO();	
		response = projectDAO.findAll();
		
		//当前没有用户
		if(response.size()<1){
			response.add("-1");
		}
		
		return response;
	}
	
	//得到所有项目
	public List<Object> GetAllProject() 
	throws SQLException, ClassNotFoundException{
		
		projectDAO = new ProjectDAO();	
		response = projectDAO.findAll();
		
		//当前没有用户
		if(response.size()<1){
			response.add("-1");
		}
		
		return response;
	}
	
	//得到某一项目的详细信息：包括模块和人员
	public List<Object> GetProjectDetailInfo(List<Object> request) 
	throws SQLException, ClassNotFoundException{
		Project project = new Project();
		project.setProid(request.get(0).toString());
		
		//通过proid取得id
		projectDAO = new ProjectDAO();
		tmp = new ArrayList<Object>();
		tmp = projectDAO.findByProid(project.getProid());
		project.setId(tmp.get(0).toString());
		tmp.clear();

		//得到项目的名称，版本
		projectDAO = new ProjectDAO();	
		tmp = projectDAO.findById(project.getId());
		
		response = new ArrayList<Object>();
		Iterator<Object> iter = tmp.iterator();
		while(iter.hasNext()){
			Project p = (Project)iter.next();
			response.add(p.getName());
			response.add(p.getVersion());
		}
		
		tmp.clear();
		
		//得到项目的模块
		ProjectModelDAO modelDAO = new ProjectModelDAO();
		tmp = modelDAO.findByProid(project.getId());
		//System.out.println("模块："+tmp.size());
		response.add(tmp.size());
		
		Iterator<Object> iter1 = tmp.iterator();
		while(iter1.hasNext()){
			Model m = (Model)iter1.next();
			response.add(m.getName());
		}
		tmp.clear();
		
		//得到项目相关的人员
		projectUserDAO = new ProjectUserDAO();
		tmp = projectUserDAO.findByProid(project.getId());
		//System.out.println("人："+tmp.size());
		response.add(tmp.size());
		
		for(Iterator<Object> it = tmp.iterator(); it.hasNext();){
			User u = (User)it.next();
			response.add(u.getUsrid());
			response.add(u.getName());
		}
		tmp.clear();
		
		//得到剩下的人员
		projectUserDAO = new ProjectUserDAO();
		tmp = projectUserDAO.findExceptProid(project.getId());
		//System.out.println("剩："+tmp.size());
		response.add(tmp.size());
		for(Iterator<Object> it = tmp.iterator(); it.hasNext();){
			User u = (User)it.next();
			response.add(u.getUsrid());
			response.add(u.getName());
		}
		tmp.clear();
		
		if(response.size()<1){
			response.add("-1");
		}
		
		return response;
	}
	
	//检查项目名是否存在
	public List<Object> CheckProid(List<Object> request) throws SQLException, ClassNotFoundException{
		Project project = new Project();
		project.setProid(request.get(0).toString());
		
		projectDAO = new ProjectDAO();
		tmp = projectDAO.findByProid(project.getProid());
		
		response = new ArrayList<Object>();

		if(tmp.size()>0)
			response.add("-2");
		else
			response.add("1");
		
		return response;
	}
	
	//添加项目
	public List<Object> AddProject(List<Object> request) 
	throws SQLException, ClassNotFoundException{
		//检查项目名是否存在
		response = CheckProid(request);
		if(response.get(0).equals("-2")){
			return response;
		}else{
			response.clear();
		}

		Project project = new Project();
		project.setProid(request.get(0).toString());
		project.setName(request.get(1).toString());
		project.setVersion(request.get(2).toString());
		
		projectDAO = new ProjectDAO();
		result = projectDAO.save(project);
		
		// Create three tables related to current project
		bugDAO = new BugDAO();
		result = bugDAO.createBugTable(project.getProid());
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//修改项目
	public List<Object> FixProject(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		Project project = new Project();
		project.setProid(request.get(0).toString());
		project.setName(request.get(1).toString());
		project.setVersion(request.get(2).toString());
		
		projectDAO = new ProjectDAO();
		result = projectDAO.update(project);
		
		response = new ArrayList<Object>();
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//检查项目的模块是否存在
	public List<Object> CheckProjectModel(Object name, Object proId) 
	throws SQLException, ClassNotFoundException{
		
		modelDAO = new ProjectModelDAO();
		tmp = modelDAO.findByNameAndProid(name, proId);

		if(tmp.size()>0)
			response.add("-2");
		else
			response.add("1");
		
		return response;
	}
	
	//添加项目的模块
	public List<Object> AddProjectModel(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		Model model = new Model();
		model.setName(request.get(0).toString());
		model.setProid(request.get(1).toString());
		
		//通过proid取得id
		projectDAO = new ProjectDAO();
		response = new ArrayList<Object>();
		response = projectDAO.findByProid(model.getProid());		
		model.setProject_id(response.get(0).toString());
		response.clear();
		
		//检查项目的模块是否存在
		response = CheckProjectModel(model.getName(),model.getProject_id());
		if(response.get(0).equals("-2")){
			return response;
		}else{
			response.clear();
		}
		
		modelDAO = new ProjectModelDAO();
		result = modelDAO.save(model.getName(), model.getProject_id());
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//删除项目的模块
	public List<Object> DeleteProjectModel(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		Model model = new Model();
		model.setName(request.get(0).toString());
		model.setProid(request.get(1).toString());
		
		//通过proid取得id
		projectDAO = new ProjectDAO();
		response = new ArrayList<Object>();
		response = projectDAO.findByProid(model.getProid());		
		model.setProject_id(response.get(0).toString());
		response.clear();
		
		modelDAO = new ProjectModelDAO();
		result = modelDAO.delete(model.getName(), Integer.parseInt(model.getProject_id()), model.getProid());
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//添加项目的用户
	public List<Object> AddProjectUser(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		ProjectUser user = new ProjectUser();
		user.setProid(request.get(0).toString());
		user.setUserid(request.get(1).toString());
		
		//通过proid取得id
		projectDAO = new ProjectDAO();
		response = new ArrayList<Object>();
		response = projectDAO.findByProid(user.getProid());		
		user.setProid(response.get(0).toString());
		response.clear();		
		
		//通过usesrid取得id
		UserManage userMan = new UserManage();
		user.setId(userMan.GetId(user.getUserid()));
		
		projectUserDAO = new ProjectUserDAO();
		result = projectUserDAO.save(user.getProid(), user.getId());
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//删除项目的用户
	public List<Object> DeleteProjectUser(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		ProjectUser user = new ProjectUser();
		user.setProid(request.get(0).toString());
		user.setUserid(request.get(1).toString());
		
		//通过proid取得id
		projectDAO = new ProjectDAO();
		response = new ArrayList<Object>();
		response = projectDAO.findByProid(user.getProid());		
		user.setProid(response.get(0).toString());
		response.clear();	
		
		//通过usesrid取得id
		UserManage userMan = new UserManage();
		user.setId(userMan.GetId(user.getUserid()));
		
		projectUserDAO = new ProjectUserDAO();
		result = projectUserDAO.delete(user.getProid(), user.getId());
		
		response = new ArrayList<Object>();
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
}
